package cn.yiufung.qxquartz.controller;

import cn.yiufung.qxquartz.common.ApiResponse;
import cn.yiufung.qxquartz.entity.SystemScheduledLogDO;
import cn.yiufung.qxquartz.service.SystemScheduledLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 姚锋
 * @createTime 2023年08月23日 11:42:00
 */
@CrossOrigin
@RestController
public class ScheduledLogController {

    private static final Logger log = LoggerFactory.getLogger(ScheduledLogController.class);

    @Autowired
    private SystemScheduledLogService systemScheduledLogService;

    @GetMapping("/scheduledLog/getList")
    public ApiResponse<PageInfo<SystemScheduledLogDO>> getList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<SystemScheduledLogDO> list = systemScheduledLogService.list(new QueryWrapper<SystemScheduledLogDO>()
                    .orderByDesc("create_time"));
            PageInfo<SystemScheduledLogDO> pageInfo = new PageInfo<>(list);
            return ApiResponse.success(pageInfo);
        }catch (Exception e){
            log.error("获取定时任务列表异常", e);
            return ApiResponse.internalServerError("请求失败");
        }
    }

    @GetMapping("/scheduledLog/getListByScheduledId")
    public ApiResponse<PageInfo<SystemScheduledLogDO>> getListByScheduledId(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "scheduledId") @NonNull Long scheduledId){
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<SystemScheduledLogDO> list = systemScheduledLogService.list(new QueryWrapper<SystemScheduledLogDO>()
                    .eq("scheduled_id", scheduledId)
                    .orderByDesc("create_time"));
            PageInfo<SystemScheduledLogDO> pageInfo = new PageInfo<>(list);
            return ApiResponse.success(pageInfo);
        }catch (Exception e){
            log.error("获取定时任务列表异常", e);
            return ApiResponse.internalServerError("请求失败");
        }
    }

}
